package cn.wolfcode.shop.cloud.web.controller;

import cn.wolfcode.shop.cloud.common.BusinessException;
import cn.wolfcode.shop.cloud.common.Result;
import cn.wolfcode.shop.cloud.domain.OrderInfo;
import cn.wolfcode.shop.cloud.domain.SeckillOrder;
import cn.wolfcode.shop.cloud.domain.User;
import cn.wolfcode.shop.cloud.mq.MQConstants;
import cn.wolfcode.shop.cloud.mq.OrderMessage;
import cn.wolfcode.shop.cloud.redis.RedisService;
import cn.wolfcode.shop.cloud.service.IOrderInfoService;
import cn.wolfcode.shop.cloud.service.ISeckillGoodService;
import cn.wolfcode.shop.cloud.util.SeckillCodeMsg;
import cn.wolfcode.shop.cloud.util.SeckillKeyPrefix;
import cn.wolfcode.shop.cloud.util.ValidationAnno;
import cn.wolfcode.shop.cloud.vo.SeckillGoodVO;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("order")
public class SeckillOrderInfoController {
    @Autowired
    private ISeckillGoodService seckillGoodService;

    @Autowired
    private IOrderInfoService orderInfoService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public static final ConcurrentHashMap<Long, Boolean> isFlag = new ConcurrentHashMap<>();

    @RequestMapping("{path}/doSeckill")
    public Result doSeckill(Long seckillId, String uuid, @PathVariable String path, @ValidationAnno User user) {
        //如果用户没有登录提醒用户违法操作
        if (user == null) {
            throw new BusinessException(SeckillCodeMsg.SECKILL_OPT);
        }
        //判断path路径是否合法
        String redisPath = redisService.get(SeckillKeyPrefix.SECKILL_PATH, uuid, String.class);
        if (!redisPath.equals(path)) {
            throw new BusinessException(SeckillCodeMsg.SECKILL_OPT);
        }
        Boolean flag = isFlag.get(seckillId);
        if (flag != null && flag) {
            throw new BusinessException(SeckillCodeMsg.SECKILL_STOCKCOUNT_OVER);
        }
        //根据秒杀id，去数据库查询是否有这么一条数据，如果没有提醒用户违法操作
        //SeckillGoodVO seckillGoodVO = seckillGoodService.getSeckillGoodById(seckillId);
        SeckillGoodVO seckillGoodVO = seckillGoodService.findByCache(seckillId);
        if (seckillGoodVO == null) {
            throw new BusinessException(SeckillCodeMsg.SECKILL_OPT);
        }
        //判断秒杀商品是否在秒杀中，没有提醒用户违法操作
        Date now = new Date();
        if (now.getTime() < seckillGoodVO.getStartDate().getTime() || now.getTime() > seckillGoodVO.getEndDate().getTime()) {
            throw new BusinessException(SeckillCodeMsg.SECKILL_OPT);
        }
        //判断用户是否重复下单，如果已经下单提醒用户不能重复下单
        //SeckillOrder seckillOrder = seckillOrderService.queryByUserIdAndSeckillId(user.getId(), seckillId);
        SeckillOrder seckillOrder = redisService.get(SeckillKeyPrefix.SECKILL_CREATE_ORDER, user.getId() + ":" + seckillId, SeckillOrder.class);
        if (seckillOrder != null) {
            throw new BusinessException(SeckillCodeMsg.SECKILL_REPEAT_ERROR);
        }
        //判断库存是否够，不够提醒用户库存不足
        /*if (seckillGoodVO.getStockCount() <= 0) {
            throw new BusinessException(SeckillCodeMsg.SECKILL_STOCKCOUNT_OVER);
        }*/
        //判断redis中是否有预库存
        if (redisService.decr(SeckillKeyPrefix.SECKILL_GOOD_STOCK, ""+seckillId) < 0) {
            isFlag.put(seckillId, true);
            throw new BusinessException(SeckillCodeMsg.SECKILL_STOCKCOUNT_OVER);
        }
        //秒杀
        //String orderNo = orderInfoService.doSeckill(user.getId(), seckillId);
        OrderMessage orderMessage = new OrderMessage();
        orderMessage.setSeckillId(seckillId);
        orderMessage.setUserId(user.getId());
        orderMessage.setUuid(uuid);
        rocketMQTemplate.syncSend(MQConstants.SECKILL_CREATE_DEST, orderMessage);

        return new Result().success("正在抢购中请稍后。。。");
    }

    @RequestMapping("getOrderInfoByOrderNo")
    public Result getOrderInfoByOrderNo(String orderNo, @ValidationAnno User user){
        if (user == null) {
            throw new BusinessException(SeckillCodeMsg.SECKILL_OPT);
        }
        OrderInfo orderInfo = orderInfoService.getOrderInfoByOrderNo(orderNo);
        if (orderInfo == null) {
            throw new BusinessException(SeckillCodeMsg.SECKILL_OPT);
        }
        //用户只能看自己的订单
        if (!orderInfo.getUserId().equals(user.getId())) {
            throw new BusinessException(SeckillCodeMsg.SECKILL_OPT);
        }
        return new Result().success(orderInfo);
    }
}
